(function($){
	$.fn.confirm = function(options){
		options = $.extend($.fn.confirm.defaults, options?options:{});
		
		function create_modal(evn){
			evn.preventDefault();
			evn.stopPropagation();
			
			var $src = $(this);
			var _url = $src.attr("href");
			if(!_url){
				_url = this.data("href");
			}
			
			var modal_str = "<div id='"+options.id+"' class='modal hide fade' tabindex='-1' data-backdrop='static' data-keyboard='false'>"
				+ "<div class='modal-header'><button type='button' class='close' data-dismiss='modal'></button><h4>"+options.title+"</h4></div>"
				+ "<div class='modal-body'><h5>"+options.message+"</h5></div>"
				+ "<div class='modal-footer'><button type='button' class='btn green ensure'>"+options.ensure+"</button><button type='button' data-dismiss='modal' class='btn cannel'>"+options.cannel+"</button></div>";
			var $modal = $(modal_str);
			
			$modal.on("shown", function(){
				var $body = $modal.find(".modal-body h5");
				var $ensure = $modal.find(".ensure");
				$ensure.bind("click", function(){
					if($.isFunction(options.confirm)){
						options.confirm.call($modal);
					}else{
						var $form = $("<form method='post' action='"+_url+"'></form>");
						$body.append($form);
						$form.submit();
					}
				});
			});
			$modal.on("hidden", function(){
				$modal.find(".ensure").unbind("click");
			});
			
			$modal.modal("show");
		}
		
		this.bind("click", create_modal);
	};
	
	$.fn.confirm.defaults = {
		id: "remote-confirm-modal",
		title: "",
		message: "",
		ensure: "确定",
		cannel: "取消",
		confirm: null
	};
})(jQuery);


